package med.movyzer.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.jfree.data.time.Millisecond;
import org.jfree.data.time.RegularTimePeriod;

@SuppressWarnings("serial")
public class ExtCalendar extends GregorianCalendar{
	public ExtCalendar(){
		super(TimeZone.getDefault());
	}
	
	public boolean isBefore(int hours,int minutes,int seconds,int milliseconds){
		ExtCalendar refTime=(ExtCalendar) this.clone();
		refTime.set(Calendar.HOUR_OF_DAY, hours);
		refTime.set(Calendar.MINUTE, minutes);
		refTime.set(Calendar.SECOND, seconds);
		refTime.set(Calendar.MILLISECOND, milliseconds);
		return this.before(refTime);
	}
	public boolean isAfter(int hours,int minutes,int seconds,int milliseconds){
		ExtCalendar refTime=(ExtCalendar) this.clone();
		refTime.set(Calendar.HOUR_OF_DAY, hours);
		refTime.set(Calendar.MINUTE, minutes);
		refTime.set(Calendar.SECOND, seconds);
		refTime.set(Calendar.MILLISECOND, milliseconds);
		return this.after(refTime);
	}
	
	public static ExtCalendar timeDiff(ExtCalendar cal1,ExtCalendar cal2){
		ExtCalendar result=(ExtCalendar) cal1.clone();
		/*result.add(Calendar.DAY_OF_MONTH,-cal2.get(Calendar.DAY_OF_MONTH));
		result.add(Calendar.MONTH,-cal2.get(Calendar.MONTH));
		result.add(Calendar.YEAR,-cal2.get(Calendar.YEAR));*/
		result.add(Calendar.MILLISECOND,-cal2.get(Calendar.MILLISECOND));
		result.add(Calendar.SECOND,-cal2.get(Calendar.SECOND));
		result.add(Calendar.MINUTE,-cal2.get(Calendar.MINUTE));
		result.add(Calendar.HOUR_OF_DAY,-cal2.get(Calendar.HOUR_OF_DAY));
		return result;
	}
	
	public void addTime(ExtCalendar time){
		this.add(Calendar.MILLISECOND, time.get(Calendar.MILLISECOND));
		this.add(Calendar.SECOND, time.get(Calendar.SECOND));
		this.add(Calendar.MINUTE, time.get(Calendar.MINUTE));
		this.add(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
	}
	
	public RegularTimePeriod getRegularTimePeriod(){
		return new Millisecond(this.getTime());
	}	
	public RegularTimePeriod getPrevRegularTimePeriod(){
		Date temp = new Date();
		temp.setTime(this.getTime().getTime()-1);
		return new Millisecond(temp);
	}	
	public RegularTimePeriod getNextRegularTimePeriod(){
		Date temp = new Date();
		temp.setTime(this.getTime().getTime()+1);
		return new Millisecond(temp);
	}	
	
	/**
	 * The hms representation of the current ExtDate. Example: 1:23:5.
	 * 
	 * @return String hh:mm:ss
	 */
	public String gethms(){
		StringBuilder sb = new StringBuilder();
		sb.append(new Integer (this.get(Calendar.HOUR_OF_DAY)).toString());
		sb.append(":");
		sb.append(new Integer (this.get(Calendar.MINUTE)).toString());
		sb.append(":");
		sb.append(new Integer (this.get(Calendar.SECOND)).toString());
		return sb.toString();
	}
	
	/**
	 * The HMS representation of the current ExtDate. Example: 01:23:05.
	 * 
	 * @return String HH:MM:SS
	 */
	public String getHMS(){
		StringBuilder sb = new StringBuilder();
		if (new Integer (this.get(Calendar.HOUR_OF_DAY)).toString().length()==1)
			sb.append("0");
		sb.append(new Integer (this.get(Calendar.HOUR_OF_DAY)).toString());
		sb.append(":");
		if (new Integer (this.get(Calendar.MINUTE)).toString().length()==1)
			sb.append("0");
		sb.append(new Integer (this.get(Calendar.MINUTE)).toString());
		sb.append(":");
		if (new Integer (this.get(Calendar.SECOND)).toString().length()==1)
			sb.append("0");
		sb.append(new Integer (this.get(Calendar.SECOND)).toString());
		return sb.toString();
	}
	
}
